package LUDOSimulator;

import java.util.ArrayList;
import java.util.List;

public class Tournament
{
	public static List<PlayerResult> playEvaluationRound(List<LUDOPlayer> players, ArrayList<LUDOPlayer> evaluationPlayers)
	{
		System.out.println("Round started");
		long time = System.currentTimeMillis();
		int[] result = new int[players.size()];
		int[] wins = new int[players.size()];
		int[] gamesPlayed = new int[players.size()];
		
		int currentPlayerNo = 0;
		
		for (LUDOPlayer currentPlayer : players)
		{
			LUDO_new.board.setPlayer(currentPlayer, LUDOBoard.YELLOW);
			LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.RED);
			LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.BLUE);
			LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.GREEN);
			int games = 100;
			try
			{
				for (int i=0; i < games; i++)
				{
					LUDO_new.board.play();
					LUDO_new.board.kill();
					
					result[currentPlayerNo]+=LUDO_new.board.getPoints()[0];

					if ((LUDO_new.board.getPoints()[0]) == 3)
						wins[currentPlayerNo] = wins[currentPlayerNo] + 1;
					
					gamesPlayed[currentPlayerNo] = gamesPlayed[currentPlayerNo] + 1;
					
					LUDO_new.board.reset();
					LUDO_new.board.setPlayer(currentPlayer, LUDOBoard.YELLOW);
					LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.RED);
					LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.BLUE);
					LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.GREEN);
					
					if ((i%games) == 0) 
						System.out.print(".");
				}
			}
			catch (InterruptedException e) { e.printStackTrace(); }
			currentPlayerNo++;
		}
		
		System.out.println("\nRound took "+(System.currentTimeMillis()-time)+" miliseconds");
		List<PlayerResult> results = new ArrayList<PlayerResult>();
		
		for (int x = 0; x < players.size(); x++)
		{
			results.add(new PlayerResult(players.get(x), x, result[x], wins[x], gamesPlayed[x]));
		}
		return results;
	}

	public static PlayerResult playFinalRound(LUDOPlayer player, ArrayList<LUDOPlayer> evaluationPlayers, int color)
	{
		System.out.println("Round started");
		long time = System.currentTimeMillis();
		int result = 0, wins = 0, gamesPlayed = 0;
		
		LUDO_new.board.setPlayer(player, color);
		
		switch(color)
		{
			case LUDOBoard.YELLOW:
				LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.RED);
				LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.BLUE);
				LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.GREEN);
				break;
			case LUDOBoard.RED:
				LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.YELLOW);
				LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.BLUE);
				LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.GREEN);
				break;
			case LUDOBoard.BLUE:
				LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.RED);
				LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.YELLOW);
				LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.GREEN);
				break;
			case LUDOBoard.GREEN:
				LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.RED);
				LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.BLUE);
				LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.YELLOW);
				break;
			default:
		}
		
		try
		{
			int games = 10000;
			for (int i=0; i < games; i++)
			{
				LUDO_new.board.play();
				LUDO_new.board.kill();
				
				result += LUDO_new.board.getPoints()[color];

				if ((LUDO_new.board.getPoints()[color]) == 3)
					wins = wins + 1;
				
				gamesPlayed = gamesPlayed + 1;
				
				LUDO_new.board.reset();

				LUDO_new.board.setPlayer(player, color);
				switch(color)
				{
					case LUDOBoard.YELLOW:
						LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.RED);
						LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.BLUE);
						LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.GREEN);
						break;
					case LUDOBoard.RED:
						LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.YELLOW);
						LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.BLUE);
						LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.GREEN);
						break;
					case LUDOBoard.BLUE:
						LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.RED);
						LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.YELLOW);
						LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.GREEN);
						break;
					case LUDOBoard.GREEN:
						LUDO_new.board.setPlayer(evaluationPlayers.get(0), LUDOBoard.RED);
						LUDO_new.board.setPlayer(evaluationPlayers.get(1), LUDOBoard.BLUE);
						LUDO_new.board.setPlayer(evaluationPlayers.get(2), LUDOBoard.YELLOW);
						break;
				}
				
				if ((i%100) == 0) 
					System.out.print(":");
			}
		}
		catch (InterruptedException e) { e.printStackTrace(); }
		
		System.out.println("\nRound took "+(System.currentTimeMillis()-time)+" miliseconds");
		return new PlayerResult(player, 0, result, wins, gamesPlayed);
	}
}
